home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / StringBuffer.java < prev    next >
Text File  |  1998-11-04  |  30KB  |  806 lines

  1. /*
  2.  * @(#)StringBuffer.java    1.36 98/10/28
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.lang;
  16.  
  17. /**
  18.  * A string buffer implements a mutable sequence of characters. 
  19.  * <p>
  20.  * String buffers are safe for use by multiple threads. The methods 
  21.  * are synchronized where necessary so that all the operations on any 
  22.  * particular instance behave as if they occur in some serial order. 
  23.  * <p>
  24.  * String buffers are used by the compiler to implement the binary 
  25.  * string concatenation operator <code>+</code>. For example, the code:
  26.  * <p><blockquote><pre>
  27.  *     x = "a" + 4 + "c"
  28.  * </pre></blockquote><p>
  29.  * is compiled to the equivalent of: 
  30.  * <p><blockquote><pre>
  31.  *     x = new StringBuffer().append("a").append(4).append("c")
  32.  *                           .toString()
  33.  * </pre></blockquote><p>
  34.  * The principal operations on a <code>StringBuffer</code> are the 
  35.  * <code>append</code> and <code>insert</code> methods, which are 
  36.  * overloaded so as to accept data of any type. Each effectively 
  37.  * converts a given datum to a string and then appends or inserts the 
  38.  * characters of that string to the string buffer. The 
  39.  * <code>append</code> method always adds these characters at the end 
  40.  * of the buffer; the <code>insert</code> method adds the characters at 
  41.  * a specified point. 
  42.  * <p>
  43.  * For example, if <code>z</code> refers to a string buffer object 
  44.  * whose current contents are "<code>start</code>", then 
  45.  * the method call <code>z.append("le")</code> would cause the string 
  46.  * buffer to contain "<code>startle</code>", whereas 
  47.  * <code>z.insert(4, "le")</code> would alter the string buffer to 
  48.  * contain "<code>starlet</code>". 
  49.  * <p>
  50.  * Every string buffer has a capacity. As long as the length of the 
  51.  * character sequence contained in the string buffer does not exceed 
  52.  * the capacity, it is not necessary to allocate a new internal 
  53.  * buffer array. If the internal buffer overflows, it is 
  54.  * automatically made larger. 
  55.  *
  56.  * @author    Arthur van Hoff
  57.  * @version     1.36, 10/28/98
  58.  * @see     java.io.ByteArrayOutputStream
  59.  * @see     java.lang.String
  60.  * @since   JDK1.0
  61.  */
  62.  
  63. public final class StringBuffer implements java.io.Serializable {
  64.     /** The value is used for character storage. */
  65.     private char value[];
  66.  
  67.     /** The count is the number of characters in the buffer. */
  68.     private int count;
  69.  
  70.     /** A flag indicating whether the buffer is shared */
  71.     private boolean shared;
  72.  
  73.     /** use serialVersionUID from JDK 1.0.2 for interoperability */
  74.     static final long serialVersionUID = 3388685877147921107L;
  75.  
  76.     /**
  77.      * Constructs a string buffer with no characters in it and an 
  78.      * initial capacity of 16 characters. 
  79.      *
  80.      * @since   JDK1.0
  81.      */
  82.     public StringBuffer() {
  83.     this(16);
  84.     }
  85.  
  86.     /**
  87.      * Constructs a string buffer with no characters in it and an 
  88.      * initial capacity specified by the <code>length</code> argument. 
  89.      *
  90.      * @param      length   the initial capacity.
  91.      * @exception  NegativeArraySizeException  if the <code>length</code>
  92.      *               argument is less than <code>0</code>.
  93.      * @since      JDK1.0
  94.      */
  95.     public StringBuffer(int length) {
  96.     value = new char[length];
  97.     shared = false;
  98.     }
  99.  
  100.     /**
  101.      * Constructs a string buffer so that it represents the same 
  102.      * sequence of characters as the string argument. The initial 
  103.      * capacity of the string buffer is <code>16</code> plus the length 
  104.      * of the string argument. 
  105.      *
  106.      * @param   str   the initial contents of the buffer.
  107.      * @since   JDK1.0
  108.      */
  109.     public StringBuffer(String str) {
  110.     this(str.length() + 16);
  111.     append(str);
  112.     }
  113.  
  114.     /**
  115.      * Returns the length (character count) of this string buffer.
  116.      *
  117.      * @return  the number of characters in this string buffer.
  118.      * @since   JDK1.0
  119.      */
  120.     public int length() {
  121.     return count;
  122.     }
  123.  
  124.     /**
  125.      * Returns the current capacity of the String buffer. The capacity
  126.      * is the amount of storage available for newly inserted
  127.      * characters; beyond which an allocation will occur.
  128.      *
  129.      * @return  the current capacity of this string buffer.
  130.      * @since   JDK1.0
  131.      */
  132.     public int capacity() {
  133.     return value.length;
  134.     }
  135.  
  136.     /**
  137.      * Copies the buffer value.  This is normally only called when shared
  138.      * is true.  It should only be called from a synchronized method.
  139.      */
  140.     private final void copy() {
  141.     char newValue[] = new char[value.length];
  142.     System.arraycopy(value, 0, newValue, 0, count);
  143.     value = newValue;
  144.     shared = false;
  145.     }
  146.  
  147.     /**
  148.      * Ensures that the capacity of the buffer is at least equal to the
  149.      * specified minimum.
  150.      * If the current capacity of this string buffer is less than the 
  151.      * argument, then a new internal buffer is allocated with greater 
  152.      * capacity. The new capacity is the larger of: 
  153.      * <ul>
  154.      * <li>The <code>minimumCapacity</code> argument. 
  155.      * <li>Twice the old capacity, plus <code>2</code>. 
  156.      * </ul>
  157.      * If the <code>minimumCapacity</code> argument is nonpositive, this
  158.      * method takes no action and simply returns.
  159.      *
  160.      * @param   minimumCapacity   the minimum desired capacity.
  161.      * @since   JDK1.0
  162.      */
  163.     public synchronized void ensureCapacity(int minimumCapacity) {
  164.     if (minimumCapacity > value.length) {
  165.         expandCapacity(minimumCapacity);
  166.     }
  167.     }
  168.  
  169.     /**
  170.      * This implements the expansion semantics of ensureCapacity but is
  171.      * unsynchronized for use internally by methods which are already
  172.      * synchronized.
  173.      *
  174.      * @see java.lang.StringBuffer#ensureCapacity(int)
  175.      */
  176.     private void expandCapacity(int minimumCapacity) {
  177.     int newCapacity = (value.length + 1) * 2;
  178.     if (minimumCapacity > newCapacity) {
  179.         newCapacity = minimumCapacity;
  180.     }
  181.     
  182.     char newValue[] = new char[newCapacity];
  183.     System.arraycopy(value, 0, newValue, 0, count);
  184.     value = newValue;
  185.     shared = false;
  186.     }
  187.  
  188.     /**
  189.      * Sets the length of this String buffer.
  190.      * If the <code>newLength</code> argument is less than the current 
  191.      * length of the string buffer, the string buffer is truncated to 
  192.      * contain exactly the number of characters given by the 
  193.      * <code>newLength</code> argument. 
  194.      * <p>
  195.      * If the <code>newLength</code> argument is greater than or equal 
  196.      * to the current length, sufficient null characters 
  197.      * (<code>'\u0000'</code>) are appended to the string buffer so that 
  198.      * length becomes the <code>newLength</code> argument. 
  199.      * <p>
  200.      * The <code>newLength</code> argument must be greater than or equal 
  201.      * to <code>0</code>. 
  202.      *
  203.      * @param      newLength   the new length of the buffer.
  204.      * @exception  StringIndexOutOfBoundsException  if the
  205.      *               <code>newLength</code> argument is invalid.
  206.      * @see        java.lang.StringBuffer#length()
  207.      * @since      JDK1.0
  208.      */
  209.     public synchronized void setLength(int newLength) {
  210.     if (newLength < 0) {
  211.         throw new StringIndexOutOfBoundsException(newLength);
  212.     }
  213.     
  214.     if (newLength > value.length) {
  215.         expandCapacity(newLength);
  216.     }
  217.  
  218.     if (count < newLength) {
  219.         if (shared) copy();
  220.         for (; count < newLength; count++) {
  221.         value[count] = '\0';
  222.         }
  223.     } else {
  224.             count = newLength;
  225.             if (shared) copy();
  226.         }
  227.     }
  228.  
  229.     /**
  230.      * Returns the character at a specific index in this string buffer. 
  231.      * <p>
  232.      * The first character of a string buffer is at index 
  233.      * <code>0</code>, the next at index <code>1</code>, and so on, for 
  234.      * array indexing. 
  235.      * <p>
  236.      * The index argument must be greater than or equal to 
  237.      * <code>0</code>, and less than the length of this string buffer. 
  238.      *
  239.      * @param      index   the index of the desired character.
  240.      * @return     the character at the specified index of this string buffer.
  241.      * @exception  StringIndexOutOfBoundsException  if the index is invalid.
  242.      * @see        java.lang.StringBuffer#length()
  243.      * @since      JDK1.0
  244.      */
  245.     public synchronized char charAt(int index) {
  246.     if ((index < 0) || (index >= count)) {
  247.         throw new StringIndexOutOfBoundsException(index);
  248.     }
  249.     return value[index];
  250.     }
  251.  
  252.     /**
  253.      * Characters are copied from this string buffer into the 
  254.      * destination character array <code>dst</code>. The first character to 
  255.      * be copied is at index <code>srcBegin</code>; the last character to 
  256.      * be copied is at index <code>srcEnd-1.</code> The total number of 
  257.      * characters to be copied is <code>srcEnd-srcBegin</code>. The 
  258.      * characters are copied into the subarray of <code>dst</code> starting 
  259.      * at index <code>dstBegin</code> and ending at index:
  260.      * <p><blockquote><pre>
  261.      *     dstbegin + (srcEnd-srcBegin) - 1
  262.      * </pre></blockquote>
  263.      *
  264.      * @param      srcBegin   start copying at this offset in the string buffer.
  265.      * @param      srcEnd     stop copying at this offset in the string buffer.
  266.      * @param      dst        the array to copy the data into.
  267.      * @param      dstBegin   offset into <code>dst</code>.
  268.      * @exception  StringIndexOutOfBoundsException  if there is an invalid
  269.      *               index into the buffer.
  270.      * @since      JDK1.0
  271.      */
  272.     public synchronized void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
  273.     if ((srcBegin < 0) || (srcBegin >= count)) {
  274.         throw new StringIndexOutOfBoundsException(srcBegin);
  275.     }
  276.     if ((srcEnd < 0) || (srcEnd > count)) {
  277.         throw new StringIndexOutOfBoundsException(srcEnd);
  278.     }
  279.     if (srcBegin < srcEnd) {
  280.         System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
  281.     }
  282.     }
  283.  
  284.     /**
  285.      * The character at the specified index of this string buffer is set 
  286.      * to <code>ch</code>. 
  287.      * <p>
  288.      * The offset argument must be greater than or equal to 
  289.      * <code>0</code>, and less than the length of this string buffer. 
  290.      *
  291.      * @param      index   the index of the character to modify.
  292.      * @param      ch      the new character.
  293.      * @exception  StringIndexOutOfBoundsException  if the index is invalid.
  294.      * @see        java.lang.StringBuffer#length()
  295.      * @since      JDK1.0
  296.      */
  297.     public synchronized void setCharAt(int index, char ch) {
  298.     if ((index < 0) || (index >= count)) {
  299.         throw new StringIndexOutOfBoundsException(index);
  300.     }
  301.     if (shared) copy();
  302.     value[index] = ch;
  303.     }
  304.  
  305.     /**
  306.      * Appends the string representation of the <code>Object</code> 
  307.      * argument to this string buffer. 
  308.      * <p>
  309.      * The argument is converted to a string as if by the method 
  310.      * <code>String.valueOf</code>, and the characters of that 
  311.      * string are then appended to this string buffer. 
  312.      *
  313.      * @param   obj   an <code>Object</code>.
  314.      * @return  this string buffer.
  315.      * @see     java.lang.String#valueOf(java.lang.Object)
  316.      * @see     java.lang.StringBuffer#append(java.lang.String)
  317.      * @since   JDK1.0
  318.      */
  319.     public synchronized StringBuffer append(Object obj) {
  320.     return append(String.valueOf(obj));
  321.     }
  322.  
  323.     /**
  324.      * Appends the string to this string buffer. 
  325.      * <p>
  326.      * The characters of the <code>String</code> argument are appended, in 
  327.      * order, to the contents of this string buffer, increasing the 
  328.      * length of this string buffer by the length of the argument. 
  329.      *
  330.      * @param   str   a string.
  331.      * @return  this string buffer.
  332.      * @since   JDK1.0
  333.      */
  334.     public synchronized StringBuffer append(String str) {
  335.     if (str == null) {
  336.         str = String.valueOf(str);
  337.     }
  338.  
  339.     int len = str.length();
  340.     int newcount = count + len;
  341.     if (newcount > value.length)
  342.         expandCapacity(newcount);
  343.     str.getChars(0, len, value, count);
  344.     count = newcount;
  345.     return this;
  346.     }
  347.  
  348.     /**
  349.      * Appends the string representation of the <code>char</code> array 
  350.      * argument to this string buffer. 
  351.      * <p>
  352.      * The characters of the array argument are appended, in order, to 
  353.      * the contents of this string buffer. The length of this string 
  354.      * buffer increases by the length of the argument. 
  355.      *
  356.      * @param   str   the characters to be appended.
  357.      * @return  this string buffer.
  358.      * @since   JDK1.0
  359.      */
  360.     public synchronized StringBuffer append(char str[]) {
  361.     int len = str.length;
  362.     int newcount = count + len;
  363.     if (newcount > value.length)
  364.         expandCapacity(newcount);
  365.     System.arraycopy(str, 0, value, count, len);
  366.     count = newcount;
  367.     return this;
  368.     }
  369.  
  370.     /**
  371.      * Appends the string representation of a subarray of the 
  372.      * <code>char</code> array argument to this string buffer. 
  373.      * <p>
  374.      * Characters of the character array <code>str</code>, starting at 
  375.      * index <code>offset</code>, are appended, in order, to the contents 
  376.      * of this string buffer. The length of this string buffer increases 
  377.      * by the value of <code>len</code>. 
  378.      *
  379.      * @param   str      the characters to be appended.
  380.      * @param   offset   the index of the first character to append.
  381.      * @param   len      the number of characters to append.
  382.      * @return  this string buffer.
  383.      * @since   JDK1.0
  384.      */
  385.     public synchronized StringBuffer append(char str[], int offset, int len) {
  386.         int newcount = count + len;
  387.     if (newcount > value.length)
  388.         expandCapacity(newcount);
  389.     System.arraycopy(str, offset, value, count, len);
  390.     count = newcount;
  391.     return this;
  392.     }
  393.  
  394.     /**
  395.      * Appends the string representation of the <code>boolean</code> 
  396.      * argument to the string buffer. 
  397.      * <p>
  398.      * The argument is converted to a string as if by the method 
  399.      * <code>String.valueOf</code>, and the characters of that 
  400.      * string are then appended to this string buffer. 
  401.      *
  402.      * @param   b   a <code>boolean</code>.
  403.      * @return  this string buffer.
  404.      * @see     java.lang.String#valueOf(boolean)
  405.      * @see     java.lang.StringBuffer#append(java.lang.String)
  406.      * @since   JDK1.0
  407.      */
  408.     public StringBuffer append(boolean b) {
  409.     return append(String.valueOf(b));
  410.     }
  411.  
  412.     /**
  413.      * Appends the string representation of the <code>char</code> 
  414.      * argument to this string buffer. 
  415.      * <p>
  416.      * The argument is appended to the contents of this string buffer. 
  417.      * The length of this string buffer increases by <code>1</code>. 
  418.      *
  419.      * @param   ch   a <code>char</code>.
  420.      * @return  this string buffer.
  421.      * @since   JDK1.0
  422.      */
  423.     public synchronized StringBuffer append(char c) {
  424.         int newcount = count + 1;
  425.     if (newcount > value.length)
  426.         expandCapacity(newcount);
  427.     value[count++] = c;
  428.     return this;
  429.     }
  430.  
  431.     /**
  432.      * Appends the string representation of the <code>int</code> 
  433.      * argument to this string buffer. 
  434.      * <p>
  435.      * The argument is converted to a string as if by the method 
  436.      * <code>String.valueOf</code>, and the characters of that 
  437.      * string are then appended to this string buffer. 
  438.      *
  439.      * @param   i   an <code>int</code>.
  440.      * @return  this string buffer.
  441.      * @see     java.lang.String#valueOf(int)
  442.      * @see     java.lang.StringBuffer#append(java.lang.String)
  443.      * @since   JDK1.0
  444.      */
  445.     public StringBuffer append(int i) {
  446.     return append(String.valueOf(i));
  447.     }
  448.  
  449.     /**
  450.      * Appends the string representation of the <code>long</code> 
  451.      * argument to this string buffer. 
  452.      * <p>
  453.      * The argument is converted to a string as if by the method 
  454.      * <code>String.valueOf</code>, and the characters of that 
  455.      * string are then appended to this string buffer. 
  456.      *
  457.      * @param   l   a <code>long</code>.
  458.      * @return  this string buffer.
  459.      * @see     java.lang.String#valueOf(long)
  460.      * @see     java.lang.StringBuffer#append(java.lang.String)
  461.      * @since   JDK1.0
  462.      */
  463.     public StringBuffer append(long l) {
  464.     return append(String.valueOf(l));
  465.     }
  466.  
  467.     /**
  468.      * Appends the string representation of the <code>float</code> 
  469.      * argument to this string buffer. 
  470.      * <p>
  471.      * The argument is converted to a string as if by the method 
  472.      * <code>String.valueOf</code>, and the characters of that 
  473.      * string are then appended to this string buffer. 
  474.      *
  475.      * @param   f   a <code>float</code>.
  476.      * @return  this string buffer.
  477.      * @see     java.lang.String#valueOf(float)
  478.      * @see     java.lang.StringBuffer#append(java.lang.String)
  479.      * @since   JDK1.0
  480.      */
  481.     public StringBuffer append(float f) {
  482.     return append(String.valueOf(f));
  483.     }
  484.  
  485.     /**
  486.      * Appends the string representation of the <code>double</code> 
  487.      * argument to this string buffer. 
  488.      * <p>
  489.      * The argument is converted to a string as if by the method 
  490.      * <code>String.valueOf</code>, and the characters of that 
  491.      * string are then appended to this string buffer. 
  492.      *
  493.      * @param   d   a <code>double</code>.
  494.      * @return  this string buffer.
  495.      * @see     java.lang.String#valueOf(double)
  496.      * @see     java.lang.StringBuffer#append(java.lang.String)
  497.      * @since   JDK1.0
  498.      */
  499.     public StringBuffer append(double d) {
  500.     return append(String.valueOf(d));
  501.     }
  502.  
  503.     /**
  504.      * Inserts the string representation of the <code>Object</code> 
  505.      * argument into this string buffer. 
  506.      * <p>
  507.      * The second argument is converted to a string as if by the method 
  508.      * <code>String.valueOf</code>, and the characters of that 
  509.      * string are then inserted into this string buffer at the indicated 
  510.      * offset. 
  511.      * <p>
  512.      * The offset argument must be greater than or equal to 
  513.      * <code>0</code>, and less than or equal to the length of this 
  514.      * string buffer. 
  515.      *
  516.      * @param      offset   the offset.
  517.      * @param      b        an <code>Object</code>.
  518.      * @return     this string buffer.
  519.      * @exception  StringIndexOutOfBoundsException  if the offset is invalid.
  520.      * @see        java.lang.String#valueOf(java.lang.Object)
  521.      * @see        java.lang.StringBuffer#insert(int, java.lang.String)
  522.      * @see        java.lang.StringBuffer#length()
  523.      * @since      JDK1.0
  524.      */
  525.     public synchronized StringBuffer insert(int offset, Object obj) {
  526.     return insert(offset, String.valueOf(obj));
  527.     }
  528.  
  529.     /**
  530.      * Inserts the string into this string buffer. 
  531.      * <p>
  532.      * The characters of the <code>String</code> argument are inserted, in 
  533.      * order, into this string buffer at the indicated offset. The length 
  534.      * of this string buffer is increased by the length of the argument. 
  535.      * <p>
  536.      * The offset argument must be greater than or equal to 
  537.      * <code>0</code>, and less than or equal to the length of this 
  538.      * string buffer. 
  539.      *
  540.      * @param      offset   the offset.
  541.      * @param      str      a string.
  542.      * @return     this string buffer.
  543.      * @exception  StringIndexOutOfBoundsException  if the offset is invalid.
  544.      * @see        java.lang.StringBuffer#length()
  545.      * @since      JDK1.0
  546.      */
  547.     public synchronized StringBuffer insert(int offset, String str) {
  548.     if ((offset < 0) || (offset > count)) {
  549.         throw new StringIndexOutOfBoundsException();
  550.     }
  551.     int len = str.length();
  552.     int newcount = count + len;
  553.     if (newcount > value.length)
  554.         expandCapacity(newcount);
  555.     else if (shared)
  556.         copy();
  557.     System.arraycopy(value, offset, value, offset + len, count - offset);
  558.     str.getChars(0, len, value, offset);
  559.     count = newcount;
  560.     return this;
  561.     }
  562.  
  563.     /**
  564.      * Inserts the string representation of the <code>char</code> array 
  565.      * argument into this string buffer. 
  566.      * <p>
  567.      * The characters of the array argument are inserted into the 
  568.      * contents of this string buffer at the position indicated by 
  569.      * <code>offset</code>. The length of this string buffer increases by 
  570.      * the length of the argument. 
  571.      *
  572.      * @param      offset   the offset.
  573.      * @param      ch       a character array.
  574.      * @return     this string buffer.
  575.      * @exception  StringIndexOutOfBoundsException  if the offset is invalid.
  576.      * @since      JDK1.0
  577.      */
  578.     public synchronized StringBuffer insert(int offset, char str[]) {
  579.     if ((offset < 0) || (offset > count)) {
  580.         throw new StringIndexOutOfBoundsException();
  581.     }
  582.     int len = str.length;
  583.     int newcount = count + len;
  584.     if (newcount > value.length)
  585.         expandCapacity(newcount);
  586.     else if (shared)
  587.         copy();
  588.     System.arraycopy(value, offset, value, offset + len, count - offset);
  589.     System.arraycopy(str, 0, value, offset, len);
  590.     count = newcount;
  591.     return this;
  592.     }
  593.  
  594.     /**
  595.      * Inserts the string representation of the <code>boolean</code> 
  596.      * argument into this string buffer. 
  597.      * <p>
  598.      * The second argument is converted to a string as if by the method 
  599.      * <code>String.valueOf</code>, and the characters of that 
  600.      * string are then inserted into this string buffer at the indicated 
  601.      * offset. 
  602.      * <p>
  603.      * The offset argument must be greater than or equal to 
  604.      * <code>0</code>, and less than or equal to the length of this 
  605.      * string buffer. 
  606.      *
  607.      * @param      offset   the offset.
  608.      * @param      b        a <code>boolean</code>.
  609.      * @return     this string buffer.
  610.      * @exception  StringIndexOutOfBoundsException  if the offset is invalid.
  611.      * @see        java.lang.String#valueOf(boolean)
  612.      * @see        java.lang.StringBuffer#insert(int, java.lang.String)
  613.      * @see        java.lang.StringBuffer#length()
  614.      * @since      JDK1.0
  615.      */
  616.     public StringBuffer insert(int offset, boolean b) {
  617.     return insert(offset, String.valueOf(b));
  618.     }
  619.  
  620.     /**
  621.      * Inserts the string representation of the <code>char</code> 
  622.      * argument into this string buffer. 
  623.      * <p>
  624.      * The second argument is inserted into the contents of this string 
  625.      * buffer at the position indicated by <code>offset</code>. The length 
  626.      * of this string buffer increases by one. 
  627.      * <p>
  628.      * The offset argument must be greater than or equal to 
  629.      * <code>0</code>, and less than or equal to the length of this 
  630.      * string buffer. 
  631.      *
  632.      * @param      offset   the offset.
  633.      * @param      ch       a <code>char</code>.
  634.      * @return     this string buffer.
  635.      * @exception  StringIndexOutOfBoundsException  if the offset is invalid.
  636.      * @see        java.lang.StringBuffer#length()
  637.      * @since      JDK1.0
  638.      */
  639.     public synchronized StringBuffer insert(int offset, char c) {
  640.     int newcount = count + 1;
  641.     if (newcount > value.length)
  642.         expandCapacity(newcount);
  643.     else if (shared)
  644.         copy();
  645.     System.arraycopy(value, offset, value, offset + 1, count - offset);
  646.     value[offset] = c;
  647.     count = newcount;
  648.     return this;
  649.     }
  650.  
  651.     /**
  652.      * Inserts the string representation of the second <code>int</code> 
  653.      * argument into this string buffer. 
  654.      * <p>
  655.      * The second argument is converted to a string as if by the method 
  656.      * <code>String.valueOf</code>, and the characters of that 
  657.      * string are then inserted into this string buffer at the indicated 
  658.      * offset. 
  659.      * <p>
  660.      * The offset argument must be greater than or equal to 
  661.      * <code>0</code>, and less than or equal to the length of this 
  662.      * string buffer. 
  663.      *
  664.      * @param      offset   the offset.
  665.      * @param      b        an <code>int</code>.
  666.      * @return     this string buffer.
  667.      * @exception  StringIndexOutOfBoundsException  if the offset is invalid.
  668.      * @see        java.lang.String#valueOf(int)
  669.      * @see        java.lang.StringBuffer#insert(int, java.lang.String)
  670.      * @see        java.lang.StringBuffer#length()
  671.      * @since      JDK1.0
  672.      */
  673.     public StringBuffer insert(int offset, int i) {
  674.     return insert(offset, String.valueOf(i));
  675.     }
  676.  
  677.     /**
  678.      * Inserts the string representation of the <code>long</code> 
  679.      * argument into this string buffer. 
  680.      * <p>
  681.      * The second argument is converted to a string as if by the method 
  682.      * <code>String.valueOf</code>, and the characters of that 
  683.      * string are then inserted into this string buffer at the indicated 
  684.      * offset. 
  685.      * <p>
  686.      * The offset argument must be greater than or equal to 
  687.      * <code>0</code>, and less than or equal to the length of this 
  688.      * string buffer. 
  689.      *
  690.      * @param      offset   the offset.
  691.      * @param      b        a <code>long</code>.
  692.      * @return     this string buffer.
  693.      * @exception  StringIndexOutOfBoundsException  if the offset is invalid.
  694.      * @see        java.lang.String#valueOf(long)
  695.      * @see        java.lang.StringBuffer#insert(int, java.lang.String)
  696.      * @see        java.lang.StringBuffer#length()
  697.      * @since      JDK1.0
  698.      */
  699.     public StringBuffer insert(int offset, long l) {
  700.     return insert(offset, String.valueOf(l));
  701.     }
  702.  
  703.     /**
  704.      * Inserts the string representation of the <code>float</code> 
  705.      * argument into this string buffer. 
  706.      * <p>
  707.      * The second argument is converted to a string as if by the method 
  708.      * <code>String.valueOf</code>, and the characters of that 
  709.      * string are then inserted into this string buffer at the indicated 
  710.      * offset. 
  711.      * <p>
  712.      * The offset argument must be greater than or equal to 
  713.      * <code>0</code>, and less than or equal to the length of this 
  714.      * string buffer. 
  715.      *
  716.      * @param      offset   the offset.
  717.      * @param      b        a <code>float</code>.
  718.      * @return     this string buffer.
  719.      * @exception  StringIndexOutOfBoundsException  if the offset is invalid.
  720.      * @see        java.lang.String#valueOf(float)
  721.      * @see        java.lang.StringBuffer#insert(int, java.lang.String)
  722.      * @see        java.lang.StringBuffer#length()
  723.      * @since      JDK1.0
  724.      */
  725.     public StringBuffer insert(int offset, float f) {
  726.     return insert(offset, String.valueOf(f));
  727.     }
  728.  
  729.     /**
  730.      * Inserts the string representation of the <code>double</code> 
  731.      * argument into this string buffer. 
  732.      * <p>
  733.      * The second argument is converted to a string as if by the method 
  734.      * <code>String.valueOf</code>, and the characters of that 
  735.      * string are then inserted into this string buffer at the indicated 
  736.      * offset. 
  737.      * <p>
  738.      * The offset argument must be greater than or equal to 
  739.      * <code>0</code>, and less than or equal to the length of this 
  740.      * string buffer. 
  741.      *
  742.      * @param      offset   the offset.
  743.      * @param      b        a <code>double</code>.
  744.      * @return     this string buffer.
  745.      * @exception  StringIndexOutOfBoundsException  if the offset is invalid.
  746.      * @see        java.lang.String#valueOf(double)
  747.      * @see        java.lang.StringBuffer#insert(int, java.lang.String)
  748.      * @see        java.lang.StringBuffer#length()
  749.      * @since      JDK1.0
  750.      */
  751.     public StringBuffer insert(int offset, double d) {
  752.     return insert(offset, String.valueOf(d));
  753.     }
  754.  
  755.     /**
  756.      * The character sequence contained in this string buffer is 
  757.      * replaced by the reverse of the sequence. 
  758.      *
  759.      * @return  this string buffer.
  760.      * @since   JDK1.0.2
  761.      */
  762.     public synchronized StringBuffer reverse() {
  763.     if (shared) copy();
  764.     int n = count - 1;
  765.     for (int j = (n-1) >> 1; j >= 0; --j) {
  766.         char temp = value[j];
  767.         value[j] = value[n - j];
  768.         value[n - j] = temp;
  769.     }
  770.     return this;
  771.     }
  772.  
  773.     /**
  774.      * Converts to a string representing the data in this string buffer.
  775.      * A new <code>String</code> object is allocated and initialized to 
  776.      * contain the character sequence currently represented by this 
  777.      * string buffer. This <code>String</code> is then returned. Subsequent 
  778.      * changes to the string buffer do not affect the contents of the 
  779.      * <code>String</code>. 
  780.      *
  781.      * @return  a string representation of the string buffer.
  782.      * @since   JDK1.0
  783.      */
  784.     public String toString() {
  785.     return new String(this);
  786.     }
  787.  
  788.     //
  789.     // The following two methods are needed by String to efficiently
  790.     // convert a StringBuffer into a String.  They are not public.
  791.     // They shouldn't be called by anyone but String.
  792.     final void setShared() { shared = true; } 
  793.     final char[] getValue() { return value; }
  794.  
  795.     /**
  796.      * readObject is called to restore the state of the StringBuffer from
  797.      * a stream.
  798.      */
  799.     private void readObject(java.io.ObjectInputStream s)
  800.          throws java.io.IOException, ClassNotFoundException {
  801.     s.defaultReadObject();
  802.     value = (char[]) value.clone();
  803.     shared = false;
  804.     }
  805. }
  806.